﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;

namespace Magento.Generation
{
    class Model : Abstract
    {
        public bool readAndCreate(string dir, string model_name)
        {
            if (!dir.EndsWith("/") && !dir.EndsWith("\\")) {
                dir += "/";
            }
            if (!Directory.Exists(dir))
            {
                this.addMessage(dir + " Directory not exist.");
                return false;
            }

            string module_name = this.getModuleName(dir);
            if (module_name == null)
                return false;
            
            if (!this.createFile(dir, module_name, model_name))
            {
                return false;
            }

            return true;
        }

        protected bool createFile(string dir, string module_name, string model_name)
        {
            var prefix = module_name.Replace("_", @"\");
            var phpClass = prefix + @"\Model\" + model_name;
            var _filePath = dir + "Model/" + model_name + ".php";
            var php = new PHP();

            // model
            var content = new List<String>();
            content.Add("    public function __construct(");
            content.Add(@"       \Magento\Framework\Model\Context $context,");
            content.Add(@"       \Magento\Framework\Registry $registry,");
            content.Add(@"       \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,");
            content.Add(@"       \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,");
            content.Add(@"       array $data = []");
            content.Add("    )");
            content.Add("    {");
            content.Add("        parent::__construct($context, $registry, $resource, $resourceCollection, $data);");
            content.Add("    }");
            content.Add("   ");
            content.Add("    public function _construct()");
            content.Add("    {");
            content.Add("        $this->_init('"+prefix+@"\Model\ResourceModel\"+ model_name + "');");
            content.Add("    }");

            if (php.setFullClassName(phpClass)
                .setExtends(@"Magento\Framework\Model\AbstractModel")
                .setMethod(content)
                .toFile(_filePath))
            {
                this.addMessage($"{_filePath} created");
            }

            // ResourceModel
            phpClass = prefix + @"\Model\ResourceModel\" + model_name;
            _filePath = dir + "Model/ResourceModel/" + model_name + ".php";
            content = new List<String>();
            php = new PHP();
            content.Add("    public function _construct()");
            content.Add("    {");
            content.Add("        $this->_init('{table_name}', 'id');");
            content.Add("    }");
            if (php.setFullClassName(phpClass)
                .setExtends(@"Magento\Framework\Model\ResourceModel\Db\AbstractDb")
                .setMethod(content)
                .toFile(_filePath))
            {
                this.addMessage($"{_filePath} created");
            }

            // Collection
            phpClass = prefix + @"\Model\ResourceModel\" + model_name+ @"\Collection";
            _filePath = dir + "Model/ResourceModel/" + model_name + "/Collection.php";
            content = new List<String>();
            php = new PHP();
            content.Add("    public function _construct()");
            content.Add("    {");
            content.Add("        $this->_init('"+ prefix + @"\Model\"+ model_name + "', '"+ prefix + @"\Model\ResourceModel\"+ model_name + "');");
            content.Add("    }");
            if (php.setFullClassName(phpClass)
                .setExtends(@"Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection")
                .setMethod(content)
                .toFile(_filePath))
            {
                this.addMessage($"{_filePath} created");
            }

            return true;
        }
    }
}
